package com.sixfoot;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.google.protobuf.ExtensionRegistry;
import com.sixfoot.handler.LoginRespClientHandler;
import com.sixfoot.hrv.codec.protobuf.LoginRespProto;
import com.sixfoot.hrv.codec.protobuf.ResourceListRespProto;
import com.sixfoot.hrv.codec.protobuf.ResponseProto;
import com.sixfoot.hrv.codec.protobuf.SaveScaleRespProto;
import com.sixfoot.hrv.codec.protobuf.ScaleListRespProto;
import com.sixfoot.hrv.codec.protobuf.ScaleRespProto;
import com.sixfoot.hrv.codec.protobuf.UpdateUserInfoRespProto;
import com.sixfoot.test.Resource;
import com.sixfoot.test.Scale;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

public class Client {
	Thread thread;
	
	public void connect(int port, String host) throws Exception {
		// 配置客户端NIO线程组
		EventLoopGroup group = new NioEventLoopGroup();
		try {
			Bootstrap b = new Bootstrap();
			b.group(group).channel(NioSocketChannel.class)
				.option(ChannelOption.TCP_NODELAY, true)
				.handler(new ChannelInitializer<SocketChannel>() {
					
					@Override
					public void initChannel(SocketChannel ch) throws Exception {
						ExtensionRegistry registry = ExtensionRegistry.newInstance();
						LoginRespProto.registerAllExtensions(registry);
						ResourceListRespProto.registerAllExtensions(registry);
						SaveScaleRespProto.registerAllExtensions(registry);
						UpdateUserInfoRespProto.registerAllExtensions(registry);
						ScaleRespProto.registerAllExtensions(registry);
						ScaleListRespProto.registerAllExtensions(registry);
						ResponseProto.registerAllExtensions(registry);
						ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
//						ch.pipeline().addLast(new ProtobufDecoder(RespFailedProto.RespFailed.getDefaultInstance()));
//						ch.pipeline().addLast(
//								new ProtobufDecoder(LoginRespSuccessProto.LoginRespSuccess.getDefaultInstance()));
						ch.pipeline().addLast(new ProtobufDecoder(ResponseProto.Response.getDefaultInstance(),
								registry));
						ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
						ch.pipeline().addLast(new ProtobufEncoder());
						ch.pipeline().addLast(new LoginRespClientHandler());
					}
					
				});
			
			// 发起异步连接操作
			Channel channel = b.connect(host, port).sync().channel();
			String line = null;
			Scale scale = null;
			// 不断读取键盘输入
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			while ((line = br.readLine()) != null) {
				if (line.equals("allResource")) {
					Resource resource = new Resource(channel);
					resource.getList();
				}
				if (line.equals("start")) {
					scale = new Scale(channel);
					scale.start();
				}
				if (line.equals("scale")) {
					scale.startScale();
				}
				if (line.equals("end")) {
					if (scale != null)
						scale.stop();
				}
				if (line.equals("scaleList")) {
					scale = new Scale(channel);
					scale.getList();
				}
				if (line.equals("scaleDetail")) {
					scale = new Scale(channel);
					scale.getScale();
				}
			}
			// 等待客户端链路关闭
			channel.closeFuture().sync();
		} finally {
			// 优雅退出，释放NIO线程组
			group.shutdownGracefully();
		}
	}
	
}
